/**
 * <pre>
 * Title: 		FragmentTag.java
 * Author:		linriqing
 * Create:	 	2010-6-9 下午04:50:50
 * Copyright: 	Copyright (c) 2010
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.common.web.tags;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.hengpeng.common.web.annotation.HtmlInput;

/**
 * <pre>
 * 自动将对象填充到HTML代码片段的标签
 * </pre>
 * @author linriqing
 * @version 1.0, 2010-6-9
 */
public class FragmentTag extends BodyTagSupport
{
	private static final long serialVersionUID = 8729160708194518463L;

	private String beanName;

	/**
	 * 构造函数
	 */
	public FragmentTag()
	{
	}

	protected String addProperties(String content)
	{
		int beginTagIndex = content.indexOf("<");
		if (beginTagIndex == -1)
		{
			return content;
		}

		int endTagIndex = content.indexOf(">", beginTagIndex);
		if (endTagIndex == -1)
		{
			return content;
		}

		if (content.charAt(endTagIndex - 1) == '/')
		{
			endTagIndex--;
		}

		StringBuffer sb = new StringBuffer();
		if ((content.indexOf("onchange") > 0) || (content.indexOf("onChange") > 0))
		{
			String t = "onchange=\"";
			int changeevent = content.indexOf("onchange=\"");
			if (changeevent < 0)
			{
				changeevent = content.indexOf("onChange=\"");
			}
			int vcstart = changeevent + t.length();
			sb.append(content.substring(0, vcstart));
			// sb.append(invoke);
			endTagIndex = content.indexOf(">");
			if (content.charAt(endTagIndex - 1) == '/')
			{
				endTagIndex--;
			}
			sb.append(content.substring(vcstart, endTagIndex));
			// modifyDefin(sb);
			sb.append(content.substring(endTagIndex));

		}
		else
		{
			sb.append(content.substring(0, endTagIndex));
			// resovleProperties(sb);
			sb.append(content.substring(endTagIndex));
		}

		return sb.toString();
	}

	public int doAfterBody() throws JspException
	{
		super.doAfterBody();
		BodyContent body = this.getBodyContent();
		String content = body.getString();
		body.clearBody();

		try
		{

			HttpServletRequest req = (HttpServletRequest) this.pageContext.getRequest();
			Object bean = req.getAttribute(beanName);
			if (bean != null)
			{
				Class<?> type = bean.getClass();
				Field[] fields = type.getDeclaredFields();
				Comparator<Field> c = new Comparator<Field>()
				{
					@Override
					public int compare(Field o1, Field o2)
					{
						o1.setAccessible(true);
						o2.setAccessible(true);
						HtmlInput htmlInput1 = o1.getAnnotation(HtmlInput.class);
						HtmlInput htmlInput2 = o2.getAnnotation(HtmlInput.class);
						if ((htmlInput1 != null) && (htmlInput2 != null))
						{
							return htmlInput1.index() - htmlInput2.index();
						}
						else
						{
							return 0;
						}
					}
				};
				Arrays.sort(fields, c);
				for (Field field : fields)
				{
					field.setAccessible(true);
				}
			}
			getPreviousOut().print(addProperties(content));
		}
		catch (IOException e)
		{
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	public int doStartTag() throws JspException
	{
		super.doStartTag();
		return EVAL_BODY_BUFFERED;
	}

	public String getBeanName()
	{
		return beanName;
	}

	public void setBeanName(String beanName)
	{
		this.beanName = beanName;
	}
}
